package com.l4.app.ui;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JPanel;

public class DrawingBoard extends JPanel {

    ArrayList<Shape> shape = new ArrayList<Shape>();
    public static Point startDrag, endDrag;
    Image image;
    static int height, width, endX, endY, startX, startY;

    public DrawingBoard(String path) throws InterruptedException {

        String fileName = path;
        image = getToolkit().getImage(fileName);
        MediaTracker mTracker = new MediaTracker(this);
        mTracker.addImage(image, 1);
        mTracker.waitForAll();
        width = image.getWidth(null);
        height = image.getHeight(null);
        System.out.println("The width of image: " + width);
        System.out.println("The height of image: " + height);
//        int panelH=img.getHeight(this);
//        int panelW=img.getWidth(null);
//        System.out.println(panelH+"-------"+panelW);
        //this.setSize(width, height);
//        this.setToolTipText("Drawing Board");

        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                startDrag = new Point(e.getX(), e.getY());

                endDrag = startDrag;
                repaint();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                Shape r = makeRectangle(startDrag.x, startDrag.y, e.getX(), e.getY());
                System.out.println(startDrag.x + "---" + startDrag.y);
                System.out.println(e.getX() + "---" + e.getY());
                startX = startDrag.x;
                startY = startDrag.y;
                endX = e.getX();
                endY = e.getY();
                ControlPanel.startBtn.setEnabled(true);
                //ControlPanel.panel.dispose();


                //ControlPanel.panel.setVisible(false);
                //MotionDetect.start(startDrag.x, startDrag.y, e.getX(), e.getY());
                //th = new MotionDetect(startDrag.x, startDrag.y, e.getX(), e.getY());
                //Thread thread = new Thread(th);
                //thread.start();

                shape.add(r);
                startDrag = null;
                endDrag = null;
                repaint();

            }
        });

        this.addMouseMotionListener(new MouseMotionAdapter() {

            @Override
            public void mouseDragged(MouseEvent e) {
                endDrag = new Point(e.getX(), e.getY());
                repaint();
            }
        });
    }

    private void paintBackground(Graphics2D g2) {

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        AffineTransform aTran = new AffineTransform();
        aTran.translate(0.0f, 0.0f);
        g2.transform(aTran);
        g2.drawImage(image, new AffineTransform(), this);
    }

    @Override
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        paintBackground(g2);
        g2.setStroke(new BasicStroke(2));
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f));

        for (Shape s : shape) {
            g2.setPaint(Color.BLUE);
            g2.draw(s);
            g2.fill(s);
        }

        if (startDrag != null && endDrag != null) {
            g2.setPaint(Color.BLACK);
            Shape r = makeRectangle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);

            g2.draw(r);

        }

    }

    private Rectangle2D.Float makeRectangle(int x1, int y1, int x2, int y2) {
        return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
    }
}
